package com.woniu.hospital.service.impl;

import com.woniu.hospital.domain.Discharge;
import com.woniu.hospital.domain.HosptilpriceType;
import com.woniu.hospital.mapper.DischargeMapper;
import com.woniu.hospital.mapper.HosptilpriceTypeMapper;
import com.woniu.hospital.service.IDischargeService;
import com.woniu.hospital.service.IHosptilpriceTypeService;
import com.woniu.hospital.service.IPatientService;
import com.woniu.hospital.vo.DischargeVo;
import com.woniu.user.domain.Patient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 出院费用总计Service业务层处理
 * 
 * @author wolf-six
 * @date 2023-02-15
 */
@Service
public class DischargeServiceImpl implements IDischargeService {
    @Autowired
    private DischargeMapper dischargeMapper;
    @Autowired
    private IHosptilpriceTypeService hosptilpriceTypeService;
    @Autowired
    private IPatientService iPatientService;

    /**
     * 查询出院费用总计
     * 
     * @param dischargeId 出院费用总计主键
     * @return 出院费用总计
     */
    @Override
    public Discharge selectDischargeByDischargeId(Long dischargeId) {
        return dischargeMapper.selectDischargeByDischargeId(dischargeId);
    }

    /**
     * 查询出院费用总计列表
     * 
     * @param discharge 出院费用总计
     * @return 出院费用总计
     */
    @Override
    @Transactional
    public List<Discharge> selectDischargeList(Discharge discharge){
        List<Discharge> list = dischargeMapper.selectDischargeList(discharge);

        //System.out.println(list);
        //List<DischargeVo> list2 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {


            /*DischargeVo dischargeVo = new DischargeVo();
            dischargeVo.setDischargeId(discharg.getDischargeId());
            dischargeVo.setDischargeStatus(discharg.getDischargeStatus());
            dischargeVo.setDischargeTime(discharg.getDischargeTime());
            dischargeVo.setDischargePrice(discharg.getDischargePrice());*/
            String name = hosptilpriceTypeService.selectHosptilpriceTypeByHosptilpriceTypeid(list.get(i).getHosptilpriceTypeid()).getHosptilpriceTypename();
            //dischargeVo.setHosptilpriceTypename(name);
            list.get(i).setHosptilpriceTypename(name);
            Patient patient = iPatientService.selectById(list.get(i).getPatientId());
            //dischargeVo.setPatientName(patient.getPatientName());
            list.get(i).setPatientName(patient.getPatientName());
            //list2.add(dischargeVo);
        }
        return list;
    }

    /**
     * 新增出院费用总计
     * 
     * @param discharge 出院费用总计
     * @return 结果
     */
    @Override
    public int insertDischarge(Discharge discharge)
    {
        return dischargeMapper.insertDischarge(discharge);
    }

    /**
     * 修改出院费用总计
     * 
     * @param discharge 出院费用总计
     * @return 结果
     */
    @Override
    public int updateDischarge(Discharge discharge)
    {
        return dischargeMapper.updateDischarge(discharge);
    }

    /**
     * 批量删除出院费用总计
     * 
     * @param dischargeIds 需要删除的出院费用总计主键
     * @return 结果
     */
    @Override
    public int deleteDischargeByDischargeIds(Long[] dischargeIds)
    {
        return dischargeMapper.deleteDischargeByDischargeIds(dischargeIds);
    }

    /**
     * 删除出院费用总计信息
     * 
     * @param dischargeId 出院费用总计主键
     * @return 结果
     */
    @Override
    public int deleteDischargeByDischargeId(Long dischargeId)
    {
        return dischargeMapper.deleteDischargeByDischargeId(dischargeId);
    }

    @Override
    @Transactional
    public void dischargeCost(Long patientId, BigDecimal cost1, BigDecimal cost2, BigDecimal cost3) {
        Discharge discharge1 = new Discharge();
        discharge1.setDischargeStatus("未缴费");
        discharge1.setDischargePrice(cost1);
        discharge1.setHosptilpriceTypeid(1l);
        discharge1.setPatientId(patientId);
        Discharge discharge2 = new Discharge();
        discharge2.setDischargeStatus("未缴费");
        discharge2.setDischargePrice(cost2);
        discharge2.setHosptilpriceTypeid(2l);
        discharge2.setPatientId(patientId);
        Discharge discharge3 = new Discharge();
        discharge3.setDischargeStatus("未缴费");
        discharge3.setDischargePrice(cost3);
        discharge3.setHosptilpriceTypeid(3l);
        discharge3.setPatientId(patientId);
        dischargeMapper.insertDischarge(discharge1);
        dischargeMapper.insertDischarge(discharge2);
        dischargeMapper.insertDischarge(discharge3);
    }


}
